import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Math.*;

/**
* RescueBoats.
* The goals of the game is to save the victims from the rooftop. 
* You can pick them up by hitting the house with your boat. The maximum
* number of victims you can rescue is 3. If you have 3 victims on board,
* then you need to drop them off at the quay. For every victim dropped off
* , you will get 4 extra seconds of time. You finished the game if you've
* saved 25 victims within the given time.
 * 
 * @author Andre de Zwart
 */
public class RescueBoats extends MiniGame
{
    private int victimCount = 0;
    private int rescuedCount = 0;
    private int setX = 0;
    private int setY = 0;
  
    House[] houseArray = new House[11];
    /**
     * Constructor for objects of class RescueBoats.
     * 
     */
    public RescueBoats()
    {    
        //Add time to counter
        super(30, 2);  

        //Create the shore
        createShore(50,50);
        
        //Create quay
        createQuay(10,10);
        
        //Spawn the player
        spawnPlayer();
        
        //Populate the world
        populateHouses(); 
    }
    
    /**
     * Act - do whatever the RescueBoats wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){                
       
        if(getGameStatus() == GameStatus.PLAYING)
        {
            
            //Populate the victims
            populateVictims();      
            
            //Stop the game if time is over
           // if(getSeconds() < 1){
           //     Greenfoot.stop();
           // }
            
            //Get the current count of rescued victims
            if(rescuedCount >= 25){
                //Game Won
                gameWon();
            }
        }
        
        super.act();
    }   
    
    /**
     * Decrease the number of spawned victims
     */
    public void victimRescued(){
        victimCount--;
    }
    
    /**
     * Increase the number of rescued victims
     */
    public void setRescuedCount(int Cnt){        
        for(int i = 0; i < Cnt; i++){
            Victim v = new Victim();
            addObject(v, ((rescuedCount + i) * 30), (getHeight() - 30));
            addScore(100);
            addSeconds(4);
        }
        
        rescuedCount += Cnt;
    }
    
    /**
     * Get the amount of rescued victims
     */
    public int getRescuedCount(){
        return rescuedCount;
    }
    
    /**
     * Create the shore
     */
    private void createShore(int setX, int setY){
        //Create the first piece of the shore
        Shore Shore1 = new Shore(setX,setY);
        addObject(Shore1, (Shore1.getWidth()/2), getHeight());
        
        //How many Shore objects do we need
        int objCount;
        objCount = (getWidth() / Shore1.getWidth());
        
        //Define Object array
        Shore[] shoreArray = new Shore[objCount];    
        
        //Create the rest of the shore
        for(int i = 0; i < objCount; i++){

            if(i == 0){
                shoreArray[i] = new Shore(setX, setY);
                addObject(shoreArray[i], Shore1.getWidth(), getHeight());
            }else{
                shoreArray[i] = new Shore(setX, setY);                
                addObject(shoreArray[i], (shoreArray[i-1].getWidth()+shoreArray[i-1].getX()), getHeight());
            }

        } 
    }
    
    /**
     * Create the Quay
     */
    private void createQuay(int setX, int setY){
        Quay q1 = new Quay();
        addObject(q1, 50, getHeight() - 65);
        
        Quay q2 = new Quay();
        addObject(q2, 50, getHeight() - 90);
    }
    
    /**
     * Spawn the player in the world
     */
    private void spawnPlayer(){
        //Create the player
        RescueBoat RB1 = new RescueBoat();
        addObject(RB1, 50, getHeight() - 120 );
    }
    
    /**
     * Populate the world with a fixed scenario of houses
     */
    private void populateHouses(){
        
        for(int i = 0; i <= 10; i++){
            houseArray[i] = new House();
            addObject(houseArray[i], 0, 0);
        
            switch(i){
                case 0:
                    setX = 370;
                    setY = 100;
                    break;            
                case 1:
                    setX = 670;
                    setY = 100;
                    break;
                case 2:
                    setX = 90;
                    setY = 180;
                    break;                
                case 3:
                    setX = 295;
                    setY = 210;
                    break;
                case 4:
                    setX = 520;
                    setY = 180;
                    break;            
                case 5:
                    setX = 85;
                    setY = 370;
                    break;
                case 6:
                    setX = 260;
                    setY = 350;
                    break;                
                case 7:
                    setX = 440;
                    setY = 315;
                    break;   
                case 8:
                    setX = 630;
                    setY = 290;
                    break;
                case 9:
                    setX = 400;
                    setY = 450;
                    break;                
                case 10:
                    setX = 670;
                    setY = 435;
                    break; 
            }

            //set the location
            houseArray[i].setLocation(setX, setY); 
        }
    }
    
    /**
     * Populate the victims on the houses
     */
    private void populateVictims(){ 
        int randNum;
        
        while(victimCount < 3){
            
            randNum = Greenfoot.getRandomNumber(11);
            
            if(!houseArray[randNum].getVictim()){
                houseArray[randNum].setVictim();
                victimCount++;
            }
            
        }
    }
}
